﻿#ifndef _UUID_H_
#define _UUID_H_


#include "kernel/Prerequisites_kernel.h"

typedef struct _timestamp_t
{
    UINT8    tm_sec;                /* Seconds after minute (0 - 59). */
    UINT8    tm_min;                /* Minutes after hour (0 - 59). */
    UINT8    tm_hour;               /* Hours after midnight (0 - 23). */
    UINT8    tm_mday;               /* Day of month (1 - 31). */
    UINT8    tm_mon;                /* Month (0 - 11; January = 0). */
    UINT8    tm_wday;               /* Day of week (0 - 6; Sunday = 0). */
    INT16    tm_year;               /* Year (current year minus 1900). */
    INT16    tm_yday;               /* Day of year (0 - 365; January 1 = 0). */
    INT32    tm_fraction;           /* Fraction little than 1 second */
} timestamp_t;

typedef struct _uuid_t
{
    UINT32    data1;
    UINT16    data2;
    UINT16    data3;
    UCHAR     data4[8];
} uuid;


/**
 * Checks whether the given string matches the UUID format.
 *    params:
 *     [in] uuid - the potential UUID string
 *    return 
 *     TRUE if the given string is a UUID, FALSE otherwise
 **/
bool is_uuid_string(const CHAR *uuid);

/**
 * Generates a new UUID. The UUID is a time-based time 1 UUID.
 * A random per-process node identifier is used to avoid keeping global
 * state and maintaining inter-process synchronization.
 **/
void uuid_create(uuid* uuid);

/**
 * Generates a new UUID string. The returned UUID is a time-based time 1 UUID.
 * A random per-process node identifier is used to avoid keeping global
 * state and maintaining inter-process synchronization.
 *  return UUID string (newly allocated)
 **/
CHAR *uuid_create_string(void);


/**
 * Translate a uuid to a uuid string
 *  return UUID string
 **/
CHAR *uuid_to_string(const uuid* uuid);

/**
 * Get timestamp from a UUID
 **/
void uuid_to_timestamp(const uuid* uuid, timestamp_t* time);


/**
 * Resurn a description of timestamp NOT including fraction
 **/
CHAR* timestamp_to_string(const timestamp_t* time);

/**
 * Compare two UUID's lexically
 *    return
 *      -1   u1 is lexically before u2
 *     0   u1 is equal to u2
 *     1   u1 is lexically after u2
*/
INT32 uuid_compare(const uuid *u1, const uuid *u2);

/**
 * Compare two UUID's temporally
 *    return
 *      -1   u1 is temporally before u2
 *     0   u1 is equal to u2
 *     1   u1 is temporally after u2
*/
INT32 uuid_compare_time(const uuid *u1, const uuid *u2);


#endif
